package com.songli.park.module;

import android.util.Log;

import com.farwolf.interfac.IFullHttp;
import com.farwolf.util.Downloader;
import com.farwolf.util.SDCard;
import com.farwolf.weex.base.WXModuleBase;
import com.farwolf.weex.util.Const;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

public class WXWalletModule extends WXModuleBase {

    @JSMethod(uiThread = false)
    public boolean isExist() {
        File file = new File(getPath());
        return file.exists();
    }

    @JSMethod(uiThread = false)
    public String read() {
        if(!isExist()){
            return "";
        }
        String content = "";
        try {
            InputStream instream = new FileInputStream(new File(getPath()));
            if (instream != null) {
                InputStreamReader inputreader
                        = new InputStreamReader(instream, "UTF-8");
                BufferedReader buffreader = new BufferedReader(inputreader);
                String line = "";
                //分行读取
                while ((line = buffreader.readLine()) != null) {
                    content += line + "\n";
                }
                instream.close();//关闭输入流
            }
        } catch (java.io.FileNotFoundException e) {
            Log.d("TestFile", "The File doesn't not exist.");
        } catch (IOException e) {
            Log.d("TestFile", e.getMessage());
        }
        return content;
    }

    @JSMethod(uiThread = false)
    public String create(String content) {
        try {
            File file = new File(getPath());
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(content.getBytes());
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Const.PREFIX_SDCARD+ getPath();
    }

    public String getPath() {
        final String path = SDCard.getBasePath(getContext()) + "/wallet/m.cer";
        return path;
    }

    @JSMethod(uiThread = false)
    public void download(String url, final JSCallback callback) {
        final String path = getPath();
        String zip = SDCard.getBasePath(getContext()) + "/wallet";
        File f = new File(zip);
        final HashMap m = new HashMap();
        if (f.exists()) {
            f.delete();
        }
        Downloader downloader = new Downloader(new IFullHttp() {
            @Override
            public void OnPostProcess(float newProgress, float current, float total) {

            }

            @Override
            public void OnPostStart(Object o) {

            }

            @Override
            public void OnPostCompelete(Object o) {

                m.put("err", 0);
                callback.invoke(m);

            }

            @Override
            public void OnException(Object o) {
                m.put("err", -1);
                callback.invoke(m);
            }
        });

        downloader.execute(url, path);
    }


}
